package com.znsd.zl.contract.controller;

import com.znsd.zl.contract.entity.ContractEntity;
import com.znsd.zl.contract.entity.ContractQueryEntity;
import com.znsd.zl.contract.openfeign.SysOssService;
import com.znsd.zl.contract.service.IContractService;
import com.znsd.zl.utils.BaseController;
import com.znsd.zl.utils.PageUtils;
import com.znsd.zl.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: ContractController
 * @Program: zlhouse-manage
 * @Description: 合同服务控制器接口
 * @Author: tangjun
 * @Date: 2020-08-18 21:07
 * @Version 1.0
 **/
@RestController
@RequestMapping("/contract")
@Slf4j
public class ContractController extends BaseController<IContractService, ContractEntity, Integer> {

	@Resource
	private SysOssService sysOssService;


	/**
	 * @Author tangjun
	 * @Description 管理员进行合同添加
	 * @Date  2020/8/30 0030
	 * @param contractQueryEntity  前台获取的主要内容
	 * @return java.lang.Integer 返回状态
	 **/
	@PostMapping("/admin")
	public Integer addAdmin(@RequestBody ContractQueryEntity contractQueryEntity) {
		System.out.println(contractQueryEntity);
		return service.addContractAdmin(contractQueryEntity);
	}

	/**
	 * @Author tangjun
	 * @Description 管理员进行合同修改
	 * @Date  2020/8/30 0030
	 * @param contractQueryEntity  前台获取的主要内容
	 * @return java.lang.Integer 返回状态
	 **/
	@PutMapping("/admin")
	public Integer updateAdmin(@RequestBody ContractQueryEntity contractQueryEntity) {
		System.out.println(contractQueryEntity);
		return service.updateAdmin(contractQueryEntity);
	}

	/**
	 * 根据id查询
	 * @param id 查询的id
	 * @return data查询的单个对象信息
	 */
	@GetMapping("/admin/{id}")
	public R getAdmin(@PathVariable("id") Integer id){
		try {
			ContractQueryEntity data= service.qeryAdmin(id);
			return R.ok().put("data", data);
		}catch (Exception e){
			e.printStackTrace();
			return R.error("服务器异常，请稍后重试");
		}
	}
	
	/**
	 * 根据id查询
	 * @param contractEntity 查询的id
	 * @return data查询的单个对象信息
	 */
	@PostMapping("/admin/getQuery")
	public R getAdminQuery(@RequestBody ContractEntity contractEntity){
		try {
			System.out.println("查看");
			System.out.println(contractEntity.toString());
			List<ContractQueryEntity> list= service.getAdminQuery(contractEntity);
			return R.ok().put("data",list);
		}catch (Exception e){
			e.printStackTrace();
			return R.error("服务器异常，请稍后重试");
		}
	}


	/**
	 * @Author tangjun
	 * @Description //TODO
	 * @Date  2020/8/31 0031
	 * @param contractEntity 前台获取的数据
	 * @return java.lang.Integer
	 **/
	@PostMapping("/add")
	public Integer add(@RequestBody ContractEntity contractEntity) {

		return service.addContract(contractEntity);
	}


	/**
	 * oss系统文件上传
	 * @param file  上传的文件
	 * @return      返回上传的文件服务器所在路径
	 */
	@PostMapping("/upload")
	public String testFileUpload(@RequestPart("file") MultipartFile file) {
		System.out.println("开始文件上传" + file);
		String s = sysOssService.feignUpload(file);
		System.out.println("执行完毕" + s);
		return sysOssService.feignUpload(file);
	}

	/**
	 * 分页查询
	 * @param params
	 * @param pageNum  当前请求页码
	 * @param pageSize 请求的记录数
	 * @return
	 */
	@Override
	@GetMapping("/{pageNum}/{pageSize}")
	public R pageFind(@RequestParam Map<String, Object> params,
	                  @PathVariable(name = "pageNum") Integer pageNum,
	                  @PathVariable(name = "pageSize") Integer pageSize) {
		PageUtils pageUtils = null;
		System.out.println("进入分页查询");
		try{
			pageUtils = service.queryContract(pageNum, pageSize, params);

		}catch (Exception e) {
			e.printStackTrace();
			return R.error("服务器异常，请稍后重试");
		}
		log.info("分页查询，pageNum===>{}，pageSize===>{}，pageUtils===>{}", pageNum, pageSize, pageUtils.toString());

		return R.ok().put("page", pageUtils);
	}

}
